import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

//导入组件
import app from '../app.vue'
//导入router入口js
import router from './router.js'

//导入vue-resource
import VueResource from 'vue-resource'
Vue.use(VueResource)
// this.$http.get("http://www.liulongbin.top:3005/api/getnewslist")
Vue.http.options.root = "http://www.liulongbin.top:3005"//全局请求http地址
// 全局设置 post 时候表单数据格式组织形式   application/x-www-form-urlencoded
Vue.http.options.emulateJSON = true;

//导入mui
import '../../lib/mui/css/mui.min.css'
import '../../lib/mui/css/icons-extra.css'
//导入mint-ui
// import{Header,Swipe,SwipeItem,Button,Lazyload}from 'mint-ui'
// Vue.component(Header.name,Header)
// Vue.component(Swipe.name,Swipe)
// Vue.component(SwipeItem.name,SwipeItem)
// Vue.component(Button.name, Button)
// Vue.use(Lazyload)
import MintUI from"mint-ui"
Vue.use(MintUI)
import 'mint-ui/lib/style.css'

// vue-preview
import VuePreview from 'vue-preview'
Vue.use(VuePreview)

//导入vuex
import Vuex from "vuex"
Vue.use(Vuex)
// 每次刚进网站，肯定会调用main.js,执行到这里，先从本地储存中，拿到购物车信息，更新到state中
let car = JSON.parse(localStorage.getItem("car") || "[]")
 const store = new Vuex.Store({
    state:{
        car:car,
    },
    mutations:{
        // 购物车商品数量同步
        changecar(state,shopcar) {
            var flag = false
            state.car.some(item=>{    
                if(item.id == shopcar.id){
                    item.count += shopcar.count
                    flag = true;
                    return true
                }
            })
            if(flag == false){
                state.car.push(shopcar)
            }

            // 当点击加入购物车后，car的数组加入本地储存
            localStorage.setItem("car",JSON.stringify(state.car))
        },
         // 修改购物车中商品的数量值
        updateGoodsInfo(state,shopcar){
            state.car.some(item=>{
                if (item.id == shopcar.id){
                    item.count = parseInt(shopcar.count)
                    return true
                }
            })
            localStorage.setItem("car",JSON.stringify(state.car))
        },
        // 删除购物车商品
        removeGoods(state,id){
            // 根据传递过来的id删除car中的数据
            state.car.some((item,i)=>{
                //这里的逻辑是找到这个id的商品，就删除它
                if(item.id== id){
                    state.car.splice(i,1)
                    return true;
                }
            })
            localStorage.setItem("car",JSON.stringify(state.car))
        },
        // 购物车中按钮状态同步
        removeshopcarbtn(state,info){
            state.car.some(item=>{
                if(item.id == info.id){
                    item.selected = info.selected
                    return true
                }
            })
            localStorage.setItem("car",JSON.stringify(state.car))
        }
    },
    getters:{
        getcount(state){
            var allcount = 0
            state.car.forEach(item => {
                allcount += item.count;
               
            });
            return allcount
        },
        updateshopcarlist(state){
            var o = {
            }
            state.car.forEach(item=>{
                o[item.id]=item.count
            })
            return o
        },
        // 获取当前car中商品的selected项并放到一个新对象中
        getGoodsSelected(state){
            var o = {}
            state.car.forEach(item=>{
               o[item.id] = item.selected
            })
            return o
        },
        getamount(state){
            var o = {
                acount : 0,
                amount : 0
            }
            state.car.forEach(item=>{
                if(item.selected){
                    o.acount += item.count
                    o.amount += item.price*item.count
                }
            })
            return o
        }
    }
})

var  vm = new Vue({
    el: '#app',
    data: {
    },
    render: c => c(app),
    router,
    store
})